package com.sinodata.bsm.cicp.collector.tomcat;

import java.lang.management.MemoryUsage;
import java.util.ArrayList;
import java.util.List;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.remote.JMXConnector;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.JmxUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class HeapCollector extends Collector {
    private static Logger logger = Logger.getLogger(HeapCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String[] result = new String[6];
        //        String port = this.getParamMap().get("端口");
        String port = this.getParamMap().get("Port");

        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];

        JMXConnector connector = null;
        MBeanServerConnection connection = null;
        try {
            //connector = JmxUtil.getConnection(JmxUtil.TOMCAT, ip + ":" + port);
            connector = JmxUtil.getConnection(ip, Integer.parseInt(port));
            connection = connector.getMBeanServerConnection();
            ObjectName heapMemory = new ObjectName("java.lang:type=Memory");
            MemoryUsage heapMemoryUsage = MemoryUsage.from((CompositeDataSupport) connection.getAttribute(heapMemory, "HeapMemoryUsage"));
            result[0] = "" + heapMemoryUsage.getInit();
            result[1] = "" + heapMemoryUsage.getUsed();
            result[2] = "" + heapMemoryUsage.getCommitted();
            result[3] = "" + heapMemoryUsage.getMax();
            result[4] = "" + heapMemoryUsage.getUsed() * 100 / heapMemoryUsage.getMax();
            result[5] = connection.getAttribute(heapMemory, "ObjectPendingFinalizationCount").toString();
            for (int i = 0; i < result.length; i++) {

                propValues[i] = ResPropertyValue.createPropValue(result[i], propMetas[i]);

            }
        } catch (Exception e) {
            logger.error("when HeapCollector:" + e);
            throw new CollectorException(e);
        } finally {
            JmxUtil.closeConnector(connector);
        }
        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
        return values;
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "名称", "端口" };
        return new String[] { "Name", "Port" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "初始堆大小", "当前堆大小", "分配的内存", "堆大小的最大值", "使用率", "暂挂结束操作对象数量" };
        //return new String[] { "InitialHeapSize", "CurrentHeapSize", "AssignedMemory", "MaxHeapSize", "Usage", "UnfinishedObjectNum" };
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6" };
    }

}
